//
//  WMEGuidePageViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/3/3.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEGuidePageViewController.h"
#import "AppDelegate.h"
@interface WMEGuidePageViewController ()<UIScrollViewAccessibilityDelegate>
@property (nonatomic, assign) BOOL isOut;
@property (nonatomic, retain) UIPageControl *page;
@end

@implementation WMEGuidePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSArray *arr = [NSArray arrayWithObjects:@"ye0",@"ye1",@"ye3", @"ye2", nil];//引导页图片数组
    UIScrollView *scr = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scr.contentSize = CGSizeMake(arr.count * self.view.frame.size.width,0);
    scr.pagingEnabled = YES;
    scr.delegate = self;
    [self.view addSubview:scr];
    //    NSLog(@"%@",arr.count);
    for (NSInteger i = 0; i < arr.count; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        imgView.image = [UIImage imageNamed:arr[i]];
        [scr addSubview:imgView];
    }
    
   
    
    // 创建pagecControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 200, 40)];
    self.page.center = CGPointMake(self.view.center.x, self.page.center.y);
    self.page.numberOfPages = 4;
    [self.view addSubview:self.page];
    [scr release];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.x > 3 * self.view.frame.size.width) {
        _isOut = YES;
    }
}



#pragma mark - 结束减速跳转主页
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.page.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
    NSLog(@"%ld", self.page.numberOfPages);
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    
    if (_isOut) {
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha = 0;
        } completion:^(BOOL finished) {
            [scrollView removeFromSuperview];
#pragma mark - 修改window的跟视图
            app.window.rootViewController = app.tabBarController;
        }];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
